/*
test_cov.c
test for index coverage: HT_TABLE_MOD == 1 
1)idx_init(CREATE)
2)idx_insert <<20
3)idx_search <<20
4)idx_delete <<5
5)idx_insert <<20
6)idx_search <<20+20
7)idx_exit()
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "defines.h"
#include "index.h"

char string_1[][20]={"aa","bb","cc","dd","ee","ff","gg","hh","ii","jj","aa0","aa1","aa2","aa3","aa4","aa5","aa6","aa7","aa8","aa9"};
char string_2[][20]={"aaa","abb","acc","add","aee","aff","agg","ahh","aii","ajj","aaa0","aaa1","aaa2","aaa3","aaa4","aaa5","aaa6","aaa7","aaa8","aaa9"};

int main()
{
	char *g_image=NULL;
	INIT_TYPE init_type;
	STATE state;
	int i, disk_location, slot_cnt, last_slot_info;

// part.1  idx_init(CREATE)
	sync_init("/tmp/disk","sync_1.log");
	g_image=(char*)mp_alloc(IMAGE_K_SIZE*1024);
	state=idx_init(INIT_TYPE_CREATE, g_image, "index_1.log", 5*1024*64);
	printf ("part_1:index_init_done\n\n");
	fflush(stdout);

// part.2  idx_insert << 20
	for (i=0; i<20; i++) idx_insert(string_1[i],strlen(string_1[i]), i,i,i);
	printf ("part_2:insert 20 done.\n\n");
	fflush(stdout);
	
// part.3  idx_search << 20
	for (i=0; i<20; i++) 	
	{
		idx_search(string_1[i], strlen(string_1[i]), &disk_location, &slot_cnt, &last_slot_info);
		printf ("%d\t%d\t%d\n", disk_location, slot_cnt, last_slot_info);
	}
	printf ("part_3:idx search 20 done.\n\n");
	fflush(stdout);
	
// part.4   idx_delete
	for (i=15; i<20; i++)
	{
		idx_delete(string_1[i], strlen(string_1[i]), &disk_location);
		printf ("%d\n", disk_location);
	}
	printf ("part_4: idx delete <<5 done\n\n");
	fflush(stdout);

// part.5   idx_insert << 20
	for (i=0; i<20; i++)
	{
		idx_insert(string_2[i], strlen(string_2[i]), i*100,i*100, i*100);
	}
	printf ("part_5: insert [2]<<20 done\n\n");
	fflush(stdout);
	
// part.6   idx_search << 20+20
	for (i=0; i<20; i++)
	{
		idx_search(string_1[i], strlen(string_1[i]), &disk_location, &slot_cnt, &last_slot_info);
		printf ("%d\t%d\t%d\n", disk_location, slot_cnt, last_slot_info);
	}
	for (i=0; i<20; i++)
	{
		idx_search(string_2[i], strlen(string_2[i]), &disk_location, &slot_cnt, &last_slot_info);
		printf ("%d\t%d\t%d\n", disk_location, slot_cnt, last_slot_info);
	}
	printf ("part.6:idx search 40 done......\n\n");
	fflush(stdout);

// part.7  idx_delete << 20+20
	for (i=0; i<20; i++)
	{
		idx_delete(string_1[i], strlen(string_1[i]), &disk_location);
		idx_delete(string_2[i], strlen(string_2[i]), &disk_location);
	}


	for (i=19; i>=0; i--)
	{
		idx_insert(string_1[i], strlen(string_1[i]), i,i,i);
		idx_insert(string_2[i], strlen(string_2[i]), i,i,i);
	}

	for (i=19; i>=0; i--)
	{
		idx_delete(string_1[i], strlen(string_1[i]), &disk_location);
	}
	for (i=0; i<20; i++)
	{
		idx_delete(string_2[i], strlen(string_2[i]), &disk_location);
	}

	printf ("part.7:idx delete 40 done......\n\n");
	fflush(stdout);

// part.7  idx_exit
	idx_exit();
	sync_exit();
	mp_free(g_image);
	printf ("part.8:exit....\n\n");
	fflush(stdout);

	return 0;
}

